package cn.demomaster.quickui.helper;


import android.os.Handler;

import cn.demomaster.quickrouter.OnReleaseListener;

//import static cn.demomaster.quickui.helper.QuickSharedPreferences.Message_Code_Last_Time;

/**
 * @author squirrel桓
 * @date 2018/11/8.
 * description：
 */
public class QuickTimer implements OnReleaseListener {

    private long startTime;
    //1.手机号，2button,3默认text,4等待text,5,onReciveSmsCode,6time
    private long totalTime = 60;
    private OnTimerListener onTimerListener;

    private QuickTimer() {

    }

    public QuickTimer(OnTimerListener onReceiveSmsCodeListener) {
        this.onTimerListener = onReceiveSmsCodeListener;
        init();
    }
    
    private void init() {
        runnable = new ValidateCodeRunnable(this);
    }

    public QuickTimer(long time, OnTimerListener onReceiveSmsCodeListener) {
        this.onTimerListener = onReceiveSmsCodeListener;
        this.totalTime = time;
        init();
    }

    public void setOnTimerListener(OnTimerListener onTimerListener) {
        this.onTimerListener = onTimerListener;
    }

    public void setTotalTime(long totalTime) {
        this.totalTime = totalTime;
        //QdLog.i("totalTime=" + totalTime);
    }

    public long getTotalTime() {
        return totalTime;
    }

    /**
     * 立即开始计时
     */
    public void start() {
        //初始化
        startTime = System.currentTimeMillis();
        //QuickSharedPreferences.getInstance().setLong(Message_Code_Last_Time, System.currentTimeMillis());
        handler.removeCallbacks(runnable);
        handler.post(runnable);
    }

    /**
     * 重置倒计时时间
     */
    public void resetTime() {
        startTime = System.currentTimeMillis();
        //QuickSharedPreferences.getInstance().setLong(Message_Code_Last_Time, System.currentTimeMillis());
        handler.removeCallbacks(runnable);
        handler.post(runnable);
    }

    public void stop() {
        if(handler!=null) {
            handler.removeCallbacks(runnable);
        }
        /*if(onTimerListener!=null) {
            onTimerListener.onTimeChange(0);
        }*/
    }

    /**
     * 重置0
     */
    public void toStart() {
        if(handler!=null) {
            handler.removeCallbacks(runnable);
        }
        if(onTimerListener!=null) {
            onTimerListener.onTimeChange(0);
        }
    }

    Handler handler = new Handler();
    ValidateCodeRunnable runnable = null;
    public static class ValidateCodeRunnable implements Runnable,OnReleaseListener{
        QuickTimer quickTimer;

        public ValidateCodeRunnable(QuickTimer quickTimer) {
            this.quickTimer = quickTimer;
        }

        @Override
        public void run() {
            long last = quickTimer.startTime;
            long now = System.currentTimeMillis();
            long diff = getSecond(now - last);
            if (diff > quickTimer.totalTime) {
                quickTimer.handler.removeCallbacks(quickTimer.runnable);
            } else {
                if (quickTimer.onTimerListener != null) {
                    quickTimer.onTimerListener.onTimeChange(quickTimer.totalTime - diff);
                }
                quickTimer.handler.postDelayed(quickTimer.runnable, 1000);
            }
        }

        @Override
        public void onRelease(Object self) {
            quickTimer.handler.removeCallbacks(quickTimer.runnable);
        }
    }
    /*private void validateCode() {
        long last = startTime;
        long now = System.currentTimeMillis();
        long diff = getSecond(now - last);
        if (diff > totalTime) {
            handler.removeCallbacks(runnable);
        } else {
            if (onTimerListener != null) {
                onTimerListener.onTimeChange(totalTime - diff);
            }
            handler.postDelayed(runnable, 1000);
        }
    }*/

    //时间戳转字符串
    public static long getSecond(long diff) {
        //以秒为单位
        return (diff / 1000);
    }

    @Override
    public void onRelease(Object self) {
        onTimerListener = null;
        if(runnable!=null){
            runnable.onRelease(self);
        }
        if (handler != null) {
            handler.removeCallbacks(runnable);
            handler.removeCallbacksAndMessages(null);
        }
    }

    public interface OnTimerListener {
        //读秒状态不可点击
        void onTimeChange(long time);
    }
}